package com.future.controller;

import javax.validation.constraints.NotNull;

import com.future.common.annotation.ControllerMapping;
import com.future.common.base.BaseController;
import com.future.dao.FoodDao;
import com.future.model.Food;
import com.future.requestbean.FoodAdd;
import com.future.requestbean.FoodUpdate;
import com.jfinal.aop.Before;
import com.jfinal.aop.Inject;
import com.jfinal.plugin.activerecord.tx.Tx;

@ControllerMapping("/food")
public class FoodController extends BaseController {
	@Inject
	private FoodDao foodDao;

	@Before(Tx.class)
	public void addFood() {
		FoodAdd foodAdd = getJsonBody(FoodAdd.class);
		Food food = beanToModel(foodAdd, Food.class);
		this.foodDao.save(food);
		renderSuccess();
	}

	@Before(Tx.class)
	public void deleteFood(@NotNull(message = "选择要删除的对象") Long foodid) {
		this.foodDao.deleteById(foodid);
		renderSuccess();
	}

	public void queryFood() {
		renderResult("foods", this.foodDao.findAll());
	}

	@Before(Tx.class)
	public void foodUpdate() {
		Food food = getJsonBodyToModel(FoodUpdate.class, Food.class);
		this.foodDao.update(food);
		renderSuccess();
	}
}
